<?php

namespace app\admin\model\repertory;

use app\common\model\repertory\Product as Models;

class Product extends Models
{
    /**
     * 获取全部
     */
    public static function getAll($deptId)
    {
        $model = new static;
        $data = $model->where('is_delete', '=', 0)->where(['dept_id' => $deptId])->with(['units', 'proclass'])->select();
        foreach($data as &$val) {
            $imageList = [];
            $imagesList = [];
            if (!empty($val["images"])) {
                $imageList = explode(",", $val["images"]);
            }
            foreach($imageList as $vals) {
                $imagesList[] = [
                    "name" => $vals,
                    "url" => $vals
                ];
            }
            $val["imagesList"] = $imagesList;
        }
        unset($val);
        return $data;
    }

    /**
     * 新增
     */
    public function add($data, $deptId)
    {
        if (isset($data["imagesList"]) && !empty($data["imagesList"])) {
            $imageList = [];
            foreach($data["imagesList"] as $val) {
                $imageList[] = $val["url"];
            }
            $data["images"] = implode(",", $imageList);
        }
        $data["dept_id"] = $deptId;
        return $this->save($data);
    }


    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 更新记录
     */
    public function edit($data)
    {
        if (isset($data["imagesList"]) && !empty($data["imagesList"])) {
            $imageList = [];
            foreach($data["imagesList"] as $val) {
                $imageList[] = $val["url"];
            }
            $data["images"] = implode(",", $imageList);
        }
        return $this->save($data);
    }

}